<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>post请求</title>
</head>
<body>
    <h1>post请求</h1>
    <form id="form">
        <input type="text" name="name">
        <input type="text" name="age">
        <button id="btn">提交内容</button>
    </form>

    <script>
        const oBtn  = document.getElementById('btn')
        const oForm = document.getElementById('form')
        const oName = document.querySelector('#form input[name=name]');
        const oAge = document.querySelector('#form input[name=age]');

        oForm.onsubmit = function(){
            //获取用户输入的信息
            const oNameData = oName.value
            const oAgeData = oAge.value
            // 创建xhr对象
            const xhr =new XMLHttpRequest();
            //打开路径
            xhr.open('POST','/adduser',true);

            //发送请求()
            xhr.send(`name=${oNameData}&age=${oAgeData}`)

            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4 && xhr.status >=200 && xhr.status<=299){
                    console.log(xhr.responseText);
                }
            }
            // 阻止表单自动提交
            return false
        }


    </script>
</body>
</html>